package com.atguigu.gmall.mq.config;

import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author: lx
 * @Time: 9:36
 * @Description: application.xml
 * <bean
 */
@Configuration
public class MQConfig {


    //延迟插件
/*    public static final String exchange_delay = "exchange.delay";
    public static final String routing_delay = "routing.delay";
    public static final String queue_delay_1 = "queue.delay.1";*/
    /////////////////////////////////////////////////////////
    //死信的
    public static final String exchange_dead = "exchange.dead";
    public static final String routing_dead_1 = "routing.dead.1";
    public static final String routing_dead_2 = "routing.dead.2";
    public static final String queue_dead_1 = "queue.dead.1";
    public static final String queue_dead_2 = "queue.dead.2";

    //配置式 第一个队列
    @Bean
    public Queue queue1() {
        Map<String, Object> arguments = new HashMap<>();
        arguments.put("x-dead-letter-exchange", exchange_dead);
        arguments.put("x-dead-letter-routing-key", routing_dead_2);
        arguments.put("x-message-ttl", 60 * 1000);//过期时间 1分钟  全局时间
        return QueueBuilder.durable(queue_dead_1).withArguments(arguments).build();
    }

    @Bean
    public Queue queue2() {
        return QueueBuilder.durable(queue_dead_2).build();
    }

    //死信交换机
    @Bean
    public Exchange exchange() {
        return ExchangeBuilder.directExchange(exchange_dead).build();
    }//发死信消息的时候 根据情况 改动时间  局部时间   优化级大于 全局时间

    //绑定
    @Bean
    public Binding binding1() {
        return BindingBuilder.bind(queue1()).to(exchange()).with(routing_dead_1).noargs();
    }
    @Bean
    public Binding binding2() {
        return BindingBuilder.bind(queue2()).to(exchange()).with(routing_dead_2).noargs();
    }
}
